﻿using IOC.Desgin;
using ORM.Model;
using ORM.Parser.Design;
using ORM.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ORM.Base
{
    public class CommandIntercept : IIntercept
    {
        public void After(object @object, string MethodName, object Result, DateTime Start, DateTime End)
        {
            int TotalTime = Convert.ToInt32((End - Start).TotalMilliseconds);
            Command cmd = (Command)@object;
            if (cmd.TraceSQL)
            {
                cmd.Trace(TotalTime);
            }

            if (ConfigUtil.IsTraceOvertTimeSQL())
            {
                if ((End-Start).TotalMilliseconds>ConfigUtil.GetTraceOverTimeLimit())
                {
                    cmd.TraceOverTime(TotalTime);
                }
              
            }
        }

        public void Before(object @object, string MethodName, object[] Parameters)
        {
            Command cmd = (Command)@object;
            if (null != cmd.GlobalTransitionID && !string.IsNullOrEmpty(cmd.GlobalTransitionID))
            {
                GlobalTransitionBuilder.GetInstance().AddGlobalTransitin(cmd.ConnectStringName, cmd.GlobalTransitionID, cmd.OptionID, cmd.GetRealSQLText());
            }
        }
    }
}
